/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;
import ohos.utils.net.Uri;

import com.tonyodev.fetch2core.Extras;
import com.tonyodev.fetch2core.FetchCoreUtils;
import java.util.Map;
import java.util.Objects;

/**
 * description Request
 *
 * @author liuluming
 * @since 2021-05-20
 */
public class Request extends RequestInfo implements Sequenceable {
    private int id;

    private String url="";

    private String file="";

    private Uri fileUri;

    CREATOR CREATOR = new Request.CREATOR();

    public  int getId() {
        return this.id;
    }

    public Uri getFileUri() {
        return FetchCoreUtils.getFileUri(this.file);
    }

    public void setFileUri(Uri fileUri) {
        this.fileUri = fileUri;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Request request = (Request) o;
        return id == request.id &&
            url.equals(request.url) &&
            file.equals(request.file) &&
            fileUri.equals(request.fileUri);
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public Request.CREATOR getCREATOR() {
        return CREATOR;
    }

    public void setCREATOR(Request.CREATOR CREATOR) {
        this.CREATOR = CREATOR;
    }
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), id, url, file, fileUri);
    }

    @Override
    public String toString() {
        return "Request{" +
            "id=" + id +
            ", url='" + url + '\'' +
            ", file='" + file + '\'' +
            ", fileUri=" + fileUri +
            '}';
    }

    public int describeContents() {
        return 0;
    }

    public final String getUrl() {
        return this.url;
    }

    public final String getFile() {
        return this.file;
    }

    public Request(String url, String file) {
        super();
        this.url = url;
        this.file = file;
        this.id = FetchCoreUtils.getUniqueId(this.url, this.file);
    }

    public Request(int id, String url, String file) {
        this.id = id;
        this.url = url;
        this.file = file;
    }
    public boolean marshalling(Parcel out) {
        return out.writeInt(id)&& out.writeString(url) && out.writeString(file);
    }

    //从包裹中读取数据
    public boolean unmarshalling(Parcel in) {
        this.id = in.readInt();
        this.url = in.readString();
        this.file = in.readString();
        return false;    //注意Uri类型的写法和普通数据类型有所不同
    }

    public static final class CREATOR implements Sequenceable.Producer {
        public Request createFromParcel(Parcel input) {
            String url = input.readString();
            String file = input.readString();
            long identifier = input.readLong();
            int groupId = input.readInt();
            Map<String, String> headers = (Map<String, String>) input.readMap();
            Priority priority = Priority.valueOf(input.readInt());
            NetworkType networkType = NetworkType.valueOf(input.readInt());
            String tag = input.readString();
            EnqueueAction enqueueAction = EnqueueAction.valueOf(input.readInt());
            boolean downloadOnEnqueue = input.readInt() == 1;
            Map<String, String> extras = (Map<String, String>) input.readMap();
            int autoRetryMaxAttempts = input.readInt();
            Request request = new Request(url, file);
            request.setIdentifier(identifier);
            request.setGroupId(groupId);
            for (String key : request.getHeaders().keySet()) {
                request.addHeader(key, request.getHeaders().get(key));
            }
            request.setHeaders(headers);
            request.setPriority(priority);
            request.setNetworkType(networkType);
            request.setTag(tag);
            request.setEnqueueAction(enqueueAction);
            request.setDownloadOnEnqueue(downloadOnEnqueue);
            request.setExtras(new Extras(extras));
            request.setAutoRetryMaxAttempts(autoRetryMaxAttempts);
            request.unmarshalling(input);
            return request;

        }
    }

    /**
     * newArray
     *
     * @param size 0
     * @return true
     */
    public Request[] newArray(int size) {
        return new Request[size];

    }
}
